<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class ajusteM extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function crearAjuste($valor, $descripcion, $idReserva, $usuario, $tipo) {
        $query = "INSERT INTO ajuste(id_reserva,fecha,valor,descripcion,usuario_encargado,tipo) VALUES('$idReserva',DATE_ADD(now(), INTERVAL -5 HOUR),'$valor','$descripcion','$usuario','$tipo')";
        return $this->db->query($query);
    }

    public function obtenerAjuste($idAjuste) {
        $query = "SELECT  valor  from ajuste WHERE id_ajuste='$idAjuste'";
        return $this->db->query($query)->result();
    }

    public function obtenerAjustes($idReserva) {
        $query = "SELECT  COALESCE(SUM(valor),0) totalAjuste from ajuste WHERE id_reserva='$idReserva'";
        return $this->db->query($query)->result();
    }
       public function obtenerSumaAjustesAlojamieno($idReserva) {
        $query = "SELECT  COALESCE(SUM(valor),0) totalAjusteAlojamiento from ajuste WHERE id_reserva='$idReserva'  AND tipo='alojamiento'";
        return $this->db->query($query)->result();
    }

    public function totalAjusteAlojamiento($idReserva) {
        $query = "SELECT  COALESCE(SUM(valor),0) totalAjuste from ajuste WHERE id_reserva='$idReserva' AND tipo='alojamiento'";
        return $this->db->query($query)->result();
    }

    public function listarAjustesByTipo($idReserva, $tipo) {
        $query = "SELECT  * from ajuste WHERE id_reserva='$idReserva' AND tipo='$tipo'";
        return $this->db->query($query)->result();
    }

    public function esMiAjuste($idAjuste, $idEstablecimiento) {
        $query = "SELECT  count(*) as cantidad from ajuste a, reserva r WHERE a.id_ajuste='$idAjuste' AND a.id_reserva=r.id_reserva AND r.id_establecimiento='$idEstablecimiento'";
        return $this->db->query($query)->result();
    }

    public function eliminarAjuste($idAjuste) {
        $query = "DELETE from ajuste where id_ajuste='$idAjuste'";
        $this->db->query($query);
    }
    
     public function listarAjustes($idReserva) {
        $query = "SELECT  * from ajuste WHERE id_reserva='$idReserva'";
        return $this->db->query($query)->result();
    }

}